﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.serverpackets;
using l1cs.server.model.Instance;
using System.Threading;
using l1cs.server.templates;

namespace l1cs.server.model
{
    class L1NpcChatTimer
    {
        private static Logger _log = Logger.getLogger( typeof( L1NpcChatTimer ).FullName );

        private L1NpcInstance _npc;

        private L1NpcChat _npcChat;

        public L1NpcChatTimer( L1NpcInstance npc, L1NpcChat npcChat )
        {
            _npc = npc;
            _npcChat = npcChat;
        }

        public void run( object state )
        {
            if ( _npc == null || _npcChat == null )
            {
                return;
            }

            if ( _npc.getHiddenStatus() != L1NpcInstance.HIDDEN_STATUS_NONE
                    || _npc._destroyed )
            {
                return;
            }

            int chatTiming = _npcChat.getChatTiming();
            int chatInterval = _npcChat.getChatInterval();
            bool isShout = _npcChat.isShout();
            bool isWorldChat = _npcChat.isWorldChat();
            String chatId1 = _npcChat.getChatId1();
            String chatId2 = _npcChat.getChatId2();
            String chatId3 = _npcChat.getChatId3();
            String chatId4 = _npcChat.getChatId4();
            String chatId5 = _npcChat.getChatId5();

            if ( !chatId1.Equals( "" ) )
            {
                chat( _npc, chatTiming, chatId1, isShout, isWorldChat );
            }

            if ( !chatId2.Equals( "" ) )
            {
                Thread.Sleep( chatInterval );
                chat( _npc, chatTiming, chatId2, isShout, isWorldChat );
            }

            if ( !chatId3.Equals( "" ) )
            {
                Thread.Sleep( chatInterval );
                chat( _npc, chatTiming, chatId3, isShout, isWorldChat );
            }

            if ( !chatId4.Equals( "" ) )
            {
                Thread.Sleep( chatInterval );
                chat( _npc, chatTiming, chatId4, isShout, isWorldChat );
            }

            if ( !chatId5.Equals( "" ) )
            {
                Thread.Sleep( chatInterval );
                chat( _npc, chatTiming, chatId5, isShout, isWorldChat );
            }
        }

        private void chat( L1NpcInstance npc, int chatTiming, String chatId,
                bool isShout, bool isWorldChat )
        {
            if ( chatTiming == L1NpcInstance.CHAT_TIMING_APPEARANCE && npc.
                    isDead() )
            {
                return;
            }
            if ( chatTiming == L1NpcInstance.CHAT_TIMING_DEAD && !npc.isDead() )
            {
                return;
            }
            if ( chatTiming == L1NpcInstance.CHAT_TIMING_HIDE && npc.isDead() )
            {
                return;
            }

            if ( !isShout )
            {
                npc.broadcastPacket( new S_NpcChatPacket( npc, chatId, 0 ) );
            }
            else
            {
                npc.wideBroadcastPacket( new S_NpcChatPacket( npc, chatId, 2 ) );
            }

            if ( isWorldChat )
            {
                // XXX npcはsendPacketsできないので、ワールド内のPCからsendPacketsさせる
                foreach ( L1PcInstance pc in L1World.getInstance().getAllPlayers() )
                {
                    if ( pc != null )
                    {
                        pc.sendPackets( new S_NpcChatPacket( npc, chatId, 3 ) );
                    }
                    break;
                }
            }
        }
    }
}
